package com.order.controller.front_end;

import com.order.dao.Cart;
import com.order.dao.Desk;
import com.order.dao.Dish;
import com.order.service.CartService;
import com.order.service.DeskService;
import com.order.service.DishService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Scope("prototype")
@AllArgsConstructor
public class CartController {

    private DeskService deskService;
    private CartService cartService;
    private DishService dishService;

    /*菜品加购*/
    @RequestMapping("/addCart/{desk_id}/{dish_id}")
    public String addCart(@PathVariable("dish_id") int dish_id, @PathVariable("desk_id") int desk_id){
        Desk desk = deskService.selectDesk(desk_id);
        Map<String,Object> map=new HashMap<>();
        map.put("dish_id",dish_id);
        map.put("desk_id",desk_id);
        int count = cartService.judgeExit(map);
        Dish dish = dishService.dishById(dish_id);
        if(count > 0){
            Cart cart=cartService.getCart(map);
            cart.setNum(cart.getNum()+1);
            cart.setTotal(cart.getNum()*dish.getPrice());
            cartService.updateCart(cart);
            log.info("购物车已更新");
        }else {
            Cart cart = new Cart();
            cart.setNum(1);
            cart.setDesk(desk);
            cart.setDish(dish);
            cart.setTotal(cart.getNum()*dish.getPrice());
            cartService.addCart(cart);
        }
        return "购物车添加成功！";

    }


    /*移除购物车里的菜品*/
    @RequestMapping("/deleteCartDish/{dish_id}")
    public String deleteCartDish(@PathVariable("dish_id") int dish_id){
        int count = cartService.deleteCartById(dish_id);
        log.info(count >0?  "删除菜品成功":"删除菜品失败");
        return (count >0?  "删除菜品成功":"删除菜品失败");
    }


    /*移除对应饭桌的购物车*/
    @RequestMapping("/deleteCart/{desk_id}")
    public String deleteCart(@PathVariable("desk_id") int desk_id){
        int count = cartService.deleteCart(desk_id);
        log.info(count >0?  "删除购物车成功":"删除购物车失败");
        return (count >0?  "删除购物车成功":"删除购物车失败");
    }


    /*查询单项购物车*/
    @RequestMapping("/getCarts/{desk_id}")
    public String myCarts(@PathVariable("desk_id") int desk_id){
        List<Cart> cartList = cartService.getCarts(desk_id);
        //    规范输出
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("菜系如下：\n");
        for (Cart d : cartList) {
            stringBuilder.append(d.toString()).append("\n");
        }
        log.info(stringBuilder.toString());

        return "查询成功";
    }


    /*修改购物车单个菜品数量*/
    @RequestMapping("/set_number/{cart_id}/{number}")
    public String setNumber(@PathVariable("cart_id")int cart_id ,@PathVariable("number")int number ){
        Cart cart = cartService.getById(cart_id);
        cart.setNum(number);
        cart.setTotal(cart.getDish().getPrice()*cart.getNum());
        int count = cartService.updateCart(cart);
        System.out.println("价格："+cart.getTotal());
        System.out.println("菜品价格："+cart.getDish().getPrice());
        System.out.println("数量："+cart.getNum());
        log.info(count>0 ? "操作成功" : "操作失败");
        return "操作成功";
    }

}
